/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     main.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-01
  * @brief    This  file include main function.
 ********************************************************************************/

#include "sys.h"
#include "common.h"
#include "uart.h"
#include "debug.h"
#include "pit.h"
#include "data.h"
#include "function_drv.h"
#include "wdt_drv.h"
#include "wav_dac.h"
#include "bsp.h"
#include "bsp_user.h"
#include "tp_rtp.h"
#include "tp_ctp.h"
#include "sd_update.h"
#include "uart_update.h"
#include "levetop.h"
#include "tftlcd.h"
#include "dma.h"
#include "iic.h"
#include "ioctrl_drv.h"
#include "usb_drv.h"
#include "usb_const.h"
#include "eflash_drv.h"
#include "iomacros.h"

uint8_t Ye_Mian_num = 0;
uint8_t  hid_rec_index;

void Check_version(void)
{
	uint8_t showbuff[10];
	uint8_t font_size = 24;
	uint8_t res = 0;

	if (encryption_key == 1)
	{
		LT268_TFT_DrawSquare_Fill(0,0,LCD_XSIZE_TFT-1,LCD_YSIZE_TFT-1,Blue2);
		LT268_TFT_ShowAscll(6,10,font_size,0,Red,White,(uint8_t*)"Illegal UI file");
		LCD_BL_Init(249);
		res = 1;
		DelayMS(2000);
	}

	if(UI_version < 1136 || UI_version >= 3000 )
	{
		LT_ReadFlash_UI(showbuff, 0x80 + 0x24, 7);
		LT268_TFT_DrawSquare_Fill(0,0,LCD_XSIZE_TFT-1,LCD_YSIZE_TFT-1,Blue2);
		LT268_TFT_ShowAscll(6,10,font_size,0,Red,White,(uint8_t*)"Firmware version warning");
		LT268_TFT_ShowAscll(6,50,font_size,0,Black,White,(uint8_t*)"MCU_Code version: V2.30");
		LT268_TFT_ShowAscll(6,90,font_size,0,Black,White,(uint8_t*)"UI_Editor-II version:  ");
		LT268_TFT_ShowAscll(6+265,90,font_size,0,Black,White,(uint8_t*)showbuff);
		LCD_BL_Init(249);
		res = 1;
		DelayMS(2000);
	}
}
	///////////////////////////////////////////////////////////////////////////////////////////////////

void PeripheralInit(void)
{
	/* system configuration��Obtain clock and other relevant parameters */
	Sys_Init();
	Enable_Interrupts;
	// SWD_Enable_Cmd(FALSE);    //SWD funtion disable
	WDT_control(0xf800, 0); // WDT funtion:0:close 1:open
	IOCTRL->SCI_CONTROL_REG |= SCICR_RX1_GINT0_SWAP_MASK;							//����Ҫ��43��ת��ΪEPORT_PIN0����16��ת����RXD1
	LT_definition_Init();

	/* uart_update check */
#if (U_MCUFLASH_FLAG == 0)
	U_McuFlash_CheckBsp();
#endif
	
	/* uart debug */
#if UART_DEBUG
	UART_Debug_Init(SCI1, g_ips_clk, 115200); // TX1:uart printf  of  baudrate 115200
#endif

	/* communication port*/
#if (UARTBUS_OPTION == 0 || UARTBUS_OPTION == 1 || UARTBUS_OPTION == 2)
	UartTFT_SCI_Init(COMuart, 115200); // 268x default SCI3 as TFT communication
#elif (UARTBUS_OPTION == 3)
	// conflict with Touch pin
	I2C_Slave_TEST(0x36);              // initialize IIC slave device address
#elif (UARTBUS_OPTION == 4)
	SPI_BUS_Initialization();
#elif (UARTBUS_OPTION == 5)
	USB_HID_Init();
#elif (UARTBUS_OPTION == 6)
	USB_CDC_Init();
#endif

	/* exSPIFlash initialization */
	exFlash_SPI_Init(); 
	if (W25N01GV_ReadID() == 0xefaa21)	{ Flash_Type = 2; W25N01GV_Init(); } 
	else                                { Flash_Type = 0; W25QXX_Init(); }

	/* get ui info */
	LT_ReadParam();

	/* communication port re-initialization */
#if (UARTBUS_OPTION == 0 || UARTBUS_OPTION == 1 || UARTBUS_OPTION == 2)
	UartTFT_SCI_Init(COMuart, gBaudrate);
#endif
#if (UARTBUS_OPTION == 2)
	Get_ModBus_CMD_info();
#endif
	
	/* mcu info printf */
#if 1
	printf("/**************************************************/\n");
	printf(" date = %s,time = %s    \r\n", (char *)__DATE__, (char *)__TIME__);
	printf(" LT268B QFN48 Demo V1.0  II  \r\n");
	printf(" system clk = %dMHz         \r\n", g_sys_clk / 1000000);
	printf(" ips clk    = %dMHz         \r\n", g_ips_clk / 1000000);
	printf("/**************************************************/\r\n");
#endif

	/* TFT init */
	LCD_TFT_Init();
//	LCD_XSIZE_TFT=240;  LCD_YSIZE_TFT=240; LCD_BL_Init(249); disp_V_Gray();  printf("OK\n");while(1);  		//RGB display(TFT test)
	
	Check_version();
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	/* pit init */
	PIT1_Init();

	/* RTC init */
	LT_RtcInit();

	/* touch init */
#if (LT_TOUCH_FLAG == 1)
	LT_TpInit();
#elif (LT_TOUCH_FLAG == 2)
	CTP_Init();
#endif

#if 1
	EFLASH_Init(g_sys_clk / 1000);
	
	if(IO_READ8(Ye_Mian_sAddr)>4)
		Ye_Mian_num = 0;
	else
		Ye_Mian_num = IO_READ8(Ye_Mian_sAddr);
	printf("Ye_Mian_num = %d \r\n",Ye_Mian_num);
	Display_page(Ye_Mian_num);
	
	if(IO_READ8(DangWei1_sAddr)>13)
		pic_flagA = 1;
	else
		pic_flagA = IO_READ8(DangWei1_sAddr);
	var[0x010C*2] = 0;
	var[0x010C*2+1] = pic_flagA;
	
	if(IO_READ8(DangWei2_sAddr)>13)
		pic_flagB = 1;
	else
		pic_flagB = IO_READ8(DangWei2_sAddr);
	var[0x0116*2] = 0;
	var[0x0116*2+1] = pic_flagB;
	
	if(IO_READ8(DangWei3_sAddr)>13)
		pic_flagC = 1;
	else
		pic_flagC = IO_READ8(DangWei3_sAddr);
	var[0x0118*2] = 0;
	var[0x0118*2+1] = pic_flagC;
	
	send500_time = 1;
#else
	/* display UI start page */
	if (Start_page >= addr_index[7])
		Display_page(0);
	else
		Display_page(Start_page);
#endif
	
	/* backlight */
	DelayMS(200);
	LCD_BL_Init(gDutyBuf[var[VAR_BL * 2 + 1]]);

	/* touch buzzer */
#if LT_TOUCH_BUZZER
	if (buzzer == 1)
		touch_buzzer_init();
#endif

	/* encoder */
#if LT_ENCODER
	Encoder_Init();
#endif

	
	/* SDcard */
#if LT_SD_UPDATE
	SD_IN_Enable();
	SD_ud_flag = SD_detection();
#endif

}
extern int pic_flagA;

int32_t main(void)
{
	/* uart IAP */
#if (U_MCUFLASH_FLAG == 1)
	NVIC_SetVectorTable(0x08043000, 0);
#else
	NVIC_SetVectorTable(0x08008000, 0);
#endif

	PeripheralInit();
	
      /*var[0x00B2*2] = pic_flagA>>8;
	  var[0x00B2*2+1] = pic_flagA&0xff;
	        var[0x020A*2] = 0X00;
			    var[0x020A*2+1] = 0X06;

				var[0x00B2*2] = 0x00;
			    var[0x00B2*2+1] = pic_flagA;*/	
/////////////////////////////////////////////////////////////////////////////////
	while (1)
	{
	

#if (UARTBUS_OPTION == 0 || UARTBUS_OPTION == 4)
		Main_From_UartRxBuff();
		if(gUsartRx.Flag)  LT_ReceiveCmd(gUsartRx.Buf);
		if(UI_update == 1)
		{ 
			while(1)
			{
				Main_From_UartRxBuff();
				if(gUsartRx.Flag)  LT_ReceiveCmd(gUsartRx.Buf);
				if(UI_update == 0) break;
			}
		}
#elif (UARTBUS_OPTION == 1)
		LT_ModBus_REG_Cmd();
#elif (UARTBUS_OPTION == 2)
		if (Sum_ModbusTX)  Uart_cmd_Send();
		LT_ModBus_REG_Cmd();
#elif (UARTBUS_OPTION == 3)
		LT_IIC_REG_Cmd(); // handle instruction function of writing register
#elif (UARTBUS_OPTION == 5 || UARTBUS_OPTION == 6)
		Hid_Main_From_UartRxBuff();
		if(gHidRx.Flag)  LT_Hid_ReceiveCmd(gHidRx.Buf);	
		if(UI_update == 1)
		{ 
			while(1)
			{
				Hid_Main_From_UartRxBuff();
				if(gHidRx.Flag)  LT_Hid_ReceiveCmd(gHidRx.Buf);	
				if(UI_update == 0) break;
			}
		}
#endif

		if (page_refreshing > 30)
		{ 
			page_refreshing = 0;
			Get_date_variable_Id_Regular_update(pic_id);
		}
		
		if(Count_reg_num > 0)
		{ 
			Count_reg_num--;
			reg_operation(Count_reg[Count_reg_num]);
		}
		
		if (gWavFlag == 1 || gWavType == 1)
			LT_PlayWav_DAC(); // WAV

#if (LT_TOUCH_FLAG == 1 || LT_TOUCH_FLAG == 2)
		gTpInfo.scan(); // Touch Point
#endif
			
		if (var[VAR_BLAUTO * 2 + 1] == 1)
			BackLight_control(); // backlight control
		
		show_gif();		 // gif
		show_Clock();	 // analog clock
		show_RTC();		 // digital clock
		scroll_text();	 // text scroll
		Display_Curve(); // curve
		show_Count();	 // time counter
		Check_Var_Count(); // timing control variable
		Check_Pointer();   // 

#if LT_ENCODER
		if (gEncoderFlag)
			encoder(); // Encoder
#endif
		//if(Ges_sc_flag)	slide_screen();  // unsupported
		if (Gesture_flag)	Gesture_touch(); // sliding gesture		
		
		Basic_touch();			// basic touch control
		Adj_touch();			// variable adjustment
		Progress_bar_sliding(); // sliding progress bar
		RingSld_touch();		// ring progress bar with touch
		slideMune();			// slide menu
		data_input();			// data input
		Ascii_input();			// ASCII keyboard
		GBK_input();			// GBK keyboard
		VarKey_touch();			// multivariable adjustment
		pop_up_touch();			// pop-up window
		

		
#if LT_SD_UPDATE //---SDcard---
		if (SD_ud_flag)
		{
			if (!SD_detection())
				Sd_To_Flash(); // SD_pin detects a low level when an SD card is inserted.
		}
		else
		{
			if (SD_detection())
				SD_ud_flag = 1;
		}
#endif
		
		/****Keyboard cursor display****/
		if (Cusor_time > 60)
		{
			uint8_t buff[2],buff1[12];
			uint32_t zk_addr,color;
			
			Cusor_time = 0;

			if (Cusor_sw == 0)
			{
				Cusor_sw++;
				buff[0] = ' ';
				buff[1] = 0;
			}
			else
			{
				Cusor_sw = 0;
				buff[0] = '|';
				buff[1] = 0;
			}
			
			if (DioKb_flag == 1)
			{
				LT_ReadFlash_UI(buff1, addr_index[2] + gDio_Info[Dio_num].zk_id * 8, 4);
				zk_addr = buff1[0] + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
				if (gDio_Info[Dio_num].Alig == 0)
					cur_x = gDio_Info[Dio_num].dp_x + gDio_Info[Dio_num].kb_x + input_w;
				else
                    cur_x = gDio_Info[Dio_num].dp_x + gDio_Info[Dio_num].kb_x - cur_w;
				
				if (gDio_Info[Dio_num].cursor_color == 0)
					color = Black;
				else
					color = White;
				
				LT_Print_zk_Font(4, zk_addr,
								 LAY_BUFF1, LCD_XSIZE_TFT, gDio_Info[Dio_num].zk_w, gDio_Info[Dio_num].zk_h, gDio_Info[Dio_num].Alig, color, Black, 0, 1, PICINFO[0], 0,
								 cur_x, cur_y, cur_x + cur_w + 1, cur_y + gDio_Info[Dio_num].zk_h -1,
								 0, 0, buff, 1);
					
			}
			else if (AioKb_flag == 1)
			{
				if (gAio_Info[Aio_num].cursor_color == 0)
					color = Black;
				else
					color = White;
				cur_x = gAio_Info[Aio_num].dp_x + gAio_Info[Aio_num].kb_x + input_w;
				
				LT_Print_zk_Font(4, gAio_Info[Aio_num].zk_addr,
								 LAY_BUFF2, LCD_XSIZE_TFT, gAio_Info[Aio_num].zk_w, gAio_Info[Aio_num].zk_h, 3, color, White, 0, 1, PICINFO[0], 0,
								 cur_x, cur_y,  cur_x + cur_w + 1, cur_y + gAio_Info[Aio_num].zk_h - 1,
								  0, 0, buff, 1);
			}
			else if (GBKioKb_flag == 1)
			{
				if (gGBKio_Info[GBKio_num].cursor_color == 0)
					color = Black;
				else
					color = White;
				cur_x = gGBKio_Info[GBKio_num].dp_x + gGBKio_Info[GBKio_num].kb_x + input_w;

				LT_Print_zk_Font(1, gGBKio_Info[GBKio_num].zk_addr,
									 LAY_BUFF2, LCD_XSIZE_TFT, gGBKio_Info[GBKio_num].zk_w, gGBKio_Info[GBKio_num].zk_h, 3, color, White, 0, 1, PICINFO[0], 0,
									 cur_x, cur_y, cur_x + cur_w + 1, cur_y + gGBKio_Info[GBKio_num].zk_h - 1, 
									 0, 0, buff, 1);
			}

		}
	}
}
